﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common;

namespace Ex01
{

    /// <summary>
    /// Problem: Captured Variable in for-loop :IndexOutOfRangeException
    /// Solution: assigning the loop variable to another variable declared inside the statement block:
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {

            string vowels = "aeiou";
            IEnumerable<char> query = "Not what you might expect";
            query=query.Where(c => c != vowels[4])
                .Where(c => c != vowels[3])
                .Where(c => c != vowels[2])
                .Where(c => c != vowels[1])
                .Where(c => c != vowels[0]);
            query.Dump();
            Console.ReadKey();
            for (int i = 0; i < vowels.Length; i++)
                query = query.Where(c => c != vowels[i]);   // IndexOutOfRangeException
            
            foreach (char c in query) Console.Write(c);



            // We can make the preceding query work correctly by assigning the loop variable to another
            // variable declared inside the statement block:

            IEnumerable<char> query1 = "Not what you might expect";
            //string vowels1 = "aeiou";

            for (int i = 0; i < vowels.Length; i++)
            {
                char vowel = vowels[i];
                query1 = query1.Where(c => c != vowel);
            }

            foreach (char c in query1) Console.Write(c);
            Console.ReadKey();
        }
    }
}
